//
//  HOTSendMessageViewController.m
//  Genpara
//
//  Created by zhang chunhui on 14-9-27.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTSendMessageViewController.h"

@interface HOTSendMessageViewController () {
    
    NSString *_uid;
}

@end

@implementation HOTSendMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil userId:(NSString *)uid
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _uid = uid;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"○○○"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    _bgImageView.image = [_bgImageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    
    [self addRightBtnTarget:self action:@selector(sendBtnClicked) image:[UIImage imageNamed:@"btn_send_off"] hImage:[UIImage imageNamed:@"btn_send_on"]];
}

-(void)sendBtnClicked {
    
    [_contentTextView resignFirstResponder];
    if (_contentTextView.text == nil || [_contentTextView.text isEqualToString:@""]) {
        
        NSString *errMsg = [self getErrMsgWith:MSG_MESSAGE_CONTENT itemType:HOTInputItemText];
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
        return;
    }
    
    [self requestUpdate];
}

-(void)requestUpdate {
    
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_TARGET_UID:_uid,
                                 REQ_KEY_MSG_CONTENT:_contentTextView.text};
    
    __unsafe_unretained HOTSendMessageViewController *tempObj = self;
    [self requestWithURL:HOT_URL_SEND_MESSAGE Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
